﻿using Newtonsoft.Json;
using SpiderStudy.Utils.Currency;
using System;
using System.Collections.Generic;

namespace SpiderStudy.WebSite.Spider360
{
    internal class GetKeys
    {
        //https://sug.so.360.cn/suggest?encodein=utf-8&encodeout=utf-8&format=json&word=%E7%99%BD%E4%B8%9D&callback=window.so.sug
        /// <summary>
        /// 360搜索API
        /// </summary>
        public string SearchAPi = "https://sug.so.360.cn/suggest?encodein=utf-8&encodeout=utf-8&format=json&word={0}";

        /// <summary>
        /// 读取全部JSON
        /// </summary>
        /// <param name="KeyWords"></param>
        /// <returns></returns>
        public List<string> ReadJson(string KeyWords)
        {
            List<string> SearchWords = new List<string>();
            String Shtml = WebHelper.HttpGet(string.Format(SearchAPi, new TextHelper().UrlEncodeToUtf8(KeyWords)));
            try
            {
                Root root = JsonConvert.DeserializeObject<Root>(Shtml);
                foreach (ResultItem SingleResultiem in root.result)
                {
                    SearchWords.Add(SingleResultiem.word);
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            Console.WriteLine(Shtml);
            return SearchWords;
        }

        public class ResultItem
        {
            /// <summary>
            /// 白丝袜学生
            /// </summary>
            public string word { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string resrc { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string source { get; set; }
        }

        public class Root
        {
            /// <summary>
            /// 白丝
            /// </summary>
            public string query { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string ext { get; set; }

            /// <summary>
            ///
            /// </summary>
            public List<ResultItem> result { get; set; }

            /// <summary>
            ///
            /// </summary>
            public string version { get; set; }
        }
    }
}